<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\Money\UserMoneyForm;
use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

class UserMemberController extends AdminController
{

    protected function title()
    {
        return "分销商管理";
    }

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->disableQuickEditButton();
            $grid->disableCreateButton();
            $titles = ['id' => '用户ID', 'nickname' => '昵称','mobile'=>"手机号",
                'money' => '余额','level_text'=>"分销商等级",'amount'=>'分销金额','used_amount'=>"已提现金额"
            ];
            $grid->export()->titles($titles);
            $grid->model()->select(["users.*"])
                ->join('user_cards',"user_cards.user_id","=","users.id")
                ->whereNotNull("users.nickname")->groupBy("users.id");
            $grid->column('id')->sortable();
            $grid->column('avatar','头像')->image(null,40,40);
            $grid->column('nickname','昵称');
            $grid->column('mobile','手机号');
            $grid->column('money','充值余额');
            $grid->column('level_text','分销商等级');
            $grid->column('amount','分销金额');
            $grid->column('used_amount','已提现金额');
            $grid->column('status','状态')->using(['启用','禁用'])->label();
            $grid->column('created_at');
            $grid->quickSearch(['id','name','nickname','mobile']);
            $grid->filter(function (Grid\Filter $filter){
                $filter->expand(false);
                $filter->panel();
                $filter->equal("id")->width(3);
                $filter->like("nickname")->width(3);
                $filter->like("name")->width(3);
                $filter->like("mobile")->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('nickname');
            $show->avatar()->image();
            $show->field('status')->using(['启用','禁用'],0);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->disableViewCheck();
            $form->disableViewButton();
            $form->text('nickname')->required();
            $form->image('avatar')->autoUpload()->autoSave(false);
//            $form->text('name','姓名');
            $form->mobile('mobile','手机号');
//            $form->currency('money','余额');
            if ($form->isEditing()){
                $button = '<button class="btn btn-info"><i class="feather icon-edit-2"></i>修改</button>';
                $modal = Modal::make()->lg()
                    ->title('修改余额')
                    ->body(UserMoneyForm::make()->payload(['user_id' =>$form->getKey()]))
                    ->button($button);
                $form->text('money','余额')->disable()->append($modal);
            }
            $form->display('amount','分销金额');
            $form->display('used_amount','已提现金额');
        });
    }
}
